package com.cloud.threads;

import java.io.IOException;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cloud.entity.ResultVo;
import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.Tools;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.LocalCouponCash;
import com.local.model.Log;
import com.local.service.face.LogService;
import com.local.util.HttpClientUtil;

/**
 * 类描述: 上传电子优惠券 的使用信息到 平台
 * 
 * @ClassName: UpDateCrossingRunble
 * @author Administrator
 * @date 2015年10月10日 下午7:45:03
 */
public class UpDateCouponCashRunble implements Runnable
{
	
	private Log4jUtil log = Log4jUtil.getLog4j(UpDateCouponCashRunble.class);
	private LocalCouponCash localCouponCash;
	private LogService logServier;
	
	public UpDateCouponCashRunble(
			com.local.model.LocalCouponCash localCouponCash,
			LogService logServier)
	{
		super();
		this.localCouponCash = localCouponCash;
		this.logServier = logServier;
	}
	
	@Override
	public void run()
	{
		// 发送请求
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		try
		{
			
			if (Tools.isEmpty(this.localCouponCash))
			{
				return;
			}
			// 获取请求路径
			String uri = CloudConfigManager.instance().getHost()
					+ CloudConfigManager.instance().getCfgValueByKey(
							"sync_coupon_cash_update");
			
			HttpPost post = new HttpPost(uri);
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			String sendData = JsonUtil.getJsonString(localCouponCash);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity(), "UTF-8");
			
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			
			if (resultVo.getResult().equals("success"))
			{
				System.out.println("上传商家抵用劵修改纪录..");
			} else
			{
				System.out.println("上传商家抵用劵修改纪录失败.");
				addErrorLog(this.localCouponCash.getCouponId(),
						"上传商家抵用劵修改纪录失败.");
			}
			
		} catch (Throwable t)
		{
			log.error("", t);
			addErrorLog(this.localCouponCash.getCouponId(), "上传商家抵用劵修改纪录失败.");
			try
			{
				if (httpClient != null)
				{
					httpClient.close();
				}
			} catch (IOException localIOException1)
			{
			}
		} finally
		{
			try
			{
				if (httpClient != null)
				{
					httpClient.close();
				}
			} catch (IOException localIOException2)
			{
			}
		}
	}
	
	public void addErrorLog(String businessUuid, String deScript)
	{
		Log logInfo = new Log();
		logInfo.setCreaterAt(new Date());
		logInfo.setSummary("修改抵用卷异常.");
		logInfo.setDescript(deScript);
		logInfo.setLevel("警告");
		logInfo.setSrc("系统");
		logInfo.setStatus(Integer.valueOf(1));
		logInfo.setCls("com.cloud.threads.UpDateCouponCashRunble");
		logInfo.setModule(Integer.valueOf(15));
		logInfo.setBusinessUuid(businessUuid);
		this.logServier.addLogInfo(logInfo);
		
	}
}
